package com.study.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.study.model.pojo.Emp;
import com.study.model.pojo.Pushmsg;
import com.study.services.EmpServices;
import com.study.services.PushmsgServices;

@RestController
@CrossOrigin
public class PushmsgController {

	@Autowired
	EmpServices es;
	@Autowired
	PushmsgServices ps;
	
	
	/**
	 * 加载发送推送消息页面（boss）*/
	@RequestMapping("/empanddept")
	public JSONObject empanddept() {
		System.out.println("进入了empanddept");
		JSONObject json = new JSONObject();
		JSONArray json1 = new JSONArray();
		List<Emp> list=es.findManager();
		for(Emp e:list){
			JSONObject jo = new JSONObject();
			jo.put("eid", e.getEid());
			jo.put("ename", e.getEname());
			json1.add(jo);			
		}
		JSONArray json2 = new JSONArray();
		List<Emp> list2=es.findEmpnob();
		for(Emp e:list2){
			JSONObject jo = new JSONObject();
			jo.put("eid", e.getEid());
			jo.put("ename", e.getEname());
			json2.add(jo);			
		}
		json.put("mngs",json1);
		json.put("emps",json2);
		System.out.print("json:"+json);
		return json;
	}
	/**经理发送的消息*/
	@RequestMapping("/send")
	public JSONObject send(String form,int pickto){
		JSONObject formx=JSONObject.parseObject(form);
		Pushmsg p=JSONObject.toJavaObject(formx, Pushmsg.class);
		if(pickto==1){
			List<Emp> list=es.findEmpnob();
			for(Emp e:list){
				p.setEid(e.getEid());
				ps.addPushmsg(p);
			}
		}else if(pickto==2){
			List<Emp> list=es.findEmpnob();
			for(Emp e:list){
				p.setEid(e.getEid());
				ps.addPushmsg(p);
			}
		}else{
			ps.addPushmsg(p);
		}
		JSONObject json = new JSONObject();
		json.put("ans","Y");
		return json;
	}
	
	/**删除推送消息*/
	@RequestMapping("/delpush")
	public JSONObject delpush(String pid) {
		ps.delPushmsg(pid);
		JSONObject json = new JSONObject();
		json.put("ans","Y");
		return json;
	}
}
